



var projects = cc.Layer.extend({
   
    Main_Node:null,
    LeftPressed:false,
    RightPressed:false,
	
	
	getActionSetuped:function( wait, direction, num, X, Y ){
		var actions = [];
		
		if( direction == "right" )
		{	
			var i=0;
			for(; i<(num-1); i++)
			{
				actions[i*2] = cc.ActionInterval.create(wait);
				actions[(i*2)+1] = cc.MoveBy.create(0.5, cc.p(-X,0) );
			}
			actions[i*2] = cc.ActionInterval.create(wait);
			actions[(i*2)+1] = cc.MoveBy.create(0.5, cc.p(X*(num-1),0) );
		}
		else if( direction == "left" )
		{	
			var i=0;
			for(; i<(num-1); i++)
			{
				actions[i*2] = cc.ActionInterval.create(wait);
				actions[(i*2)+1] = cc.MoveBy.create(0.5, cc.p(X,0) );
			}
			actions[i*2] = cc.ActionInterval.create(wait);
			actions[(i*2)+1] = cc.MoveBy.create(0.5, cc.p(-X*(num-1),0) );
		}
		else if( direction == "up" )
		{	
			var i=0;
			for(; i<(num-1); i++)
			{
				actions[i*2] = cc.ActionInterval.create(wait);
				actions[(i*2)+1] = cc.MoveBy.create(0.5, cc.p(0,-Y) );
			}
			actions[i*2] = cc.ActionInterval.create(wait);
			actions[(i*2)+1] = cc.MoveBy.create(0.5, cc.p(0,Y*(num-1)) );
		}
		else if( direction == "down" )
		{	
			var i=0;
			for(; i<(num-1); i++)
			{
				actions[i*2] = cc.ActionInterval.create(wait);
				actions[(i*2)+1] = cc.MoveBy.create(0.5, cc.p(0,Y) );
			}
			actions[i*2] = cc.ActionInterval.create(wait);
			actions[(i*2)+1] = cc.MoveBy.create(0.5, cc.p(0,-Y*(num-1)) );
		}
		
		var seq = cc.Sequence.create(actions);
		var repeat = cc.RepeatForever.create(seq);
		
		return repeat;
	},
	
    init:function () {
        var selfPointer = this;
        //////////////////////////////
        // 1. super init first
        this._super();

		this.setTouchEnabled(true);
		this.setKeyboardEnabled(true);
        this.scheduleUpdate();
		var size = cc.Director.getInstance().getWinSize();
		
                               
        var tmp = null;
		
        // size 1350x450
        for(var i=0; i<11; i++)
        {
            tmp = cc.CustomSprite.create("res/museum_background.png");
            tmp.setPosition( cc.p(1350*i,0) );
            this.addChild(tmp);
        }
		

		var tmp2 = null;
		
		
		
		
		
		
		
		
		
		// Pirate Wings Projects
		tmp = cc.FrameNode.create();
		tmp.setPosition( cc.p(200,150) );
		this.addChild(tmp);
                   
		// frame 1
		tmp2 = cc.CustomSprite.create("res/projects/pw_frame1.png");
			tmp2.setPosition( cc.p(-200,0) );
			tmp2.runAction( this.getActionSetuped(7.0, "left", 3, 100, 56 ) );
		tmp._Frames[0].addChild( tmp2 );
		
		// frame 2
		tmp2 = cc.CustomSprite.create("res/projects/pw_frame2.png");
			tmp2.setPosition( cc.p(0,0) );
			tmp2.runAction( this.getActionSetuped(6.0, "up", 2, 87, 99 ) );
		tmp._Frames[1].addChild( tmp2 );
		
		// frame 3
		tmp2 = cc.CustomSprite.create("res/projects/pw_frame3.png");
			tmp2.setPosition( cc.p(0,0) );
			//tmp2.runAction( this.getActionSetuped(5.0, "right", 1, 39, 84 ) );
		tmp._Frames[2].addChild( tmp2 );

		// frame 4
		tmp2 = cc.CustomSprite.create("res/projects/pw_frame4.png");
			tmp2.setPosition( cc.p(0,0) );
			tmp2.runAction( this.getActionSetuped(5.0, "right", 3, 167, 111 ) );
		tmp._Frames[3].addChild( tmp2 );

				
				
				
		
		
		
		
		
		
		
		// wrc Projects
		tmp = cc.FrameNode.create();
		tmp.setPosition( cc.p(1300,150) );
		this.addChild(tmp);
                   
		// frame 1
		tmp2 = cc.CustomSprite.create("res/projects/wrc_frame1.png");
			tmp2.setPosition( cc.p(0,0) );
			tmp2.runAction( this.getActionSetuped(4.0, "right", 7, 100, 56 ) );
		tmp._Frames[0].addChild( tmp2 );
		
		// frame 2
		tmp2 = cc.CustomSprite.create("res/projects/wrc_frame2.png");
			tmp2.setPosition( cc.p(0,-99*5) );
			tmp2.runAction( this.getActionSetuped(7.0, "down", 6, 87, 99 ) );
		tmp._Frames[1].addChild( tmp2 );
		
		// frame 3
		tmp2 = cc.CustomSprite.create("res/projects/wrc_frame3.png");
			tmp2.setPosition( cc.p(0,0) );
			tmp2.runAction( this.getActionSetuped(9.0, "up", 3, 39, 84 ) );
		tmp._Frames[2].addChild( tmp2 );

		// frame 4
		tmp2 = cc.CustomSprite.create("res/projects/wrc_frame4.png");
			tmp2.setPosition( cc.p(-167*6,0) );
			tmp2.runAction( this.getActionSetuped(5.0, "left", 7, 167, 111 ) );
		tmp._Frames[3].addChild( tmp2 );

		
		
		






				
		//Menu
		var items = new Array();
		var p = cc.p( (-size.width/2) + 20 ,
					  (-size.height/2) + 30 );
		items[0] = createButton("Back");
		items[0].setPosition( p );
		items[0].setCallback(this,
            function () {
				myApp.GoToScene( new HelloWorldScene() );
            });
		var myMenu = cc.MenuCustom.create(items);
		this.addChild(myMenu);
		
        return true;
    },
    
    update:function(dt){
		var speed = 500.0;
        
        if( this.RightPressed )
        {
            if( this.getPosition().x > -13500.0 )
            {
                this.setPosition( cc.p(this.getPosition().x - speed*dt, 0.0) );
                                    
                if( this.getPosition().x < -13500.0 )
                    this.setPosition( cc.p(-13500.0,0.0) );
            }
        }
        
        if( this.LeftPressed )
        {
            if( this.getPosition().x < 0.0 )
            {
                this.setPosition(cc.p(this.getPosition().x + speed*dt, 0.0) );
                                    
                if( this.getPosition().x > 0.0 )
                    this.setPosition( cc.p(0.0,0.0) );
            }
        }
	},
	
	onTouchesEnded:function (pTouch,pEvent){
    },
    onTouchesMoved:function(pTouch,pEvent){
    },
    onKeyUp:function(e){
        if(e === cc.KEY.left)
        {
            this.LeftPressed = false;
        }
        else if(e === cc.KEY.right)
        {
            this.RightPressed = false;
        }
    },
    onKeyDown:function(e){
        if(e === cc.KEY.left)
        {
            this.LeftPressed = true;
        }
        else if(e === cc.KEY.right)
        {
            this.RightPressed = true;
        }
    },
});


var projectsScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new projects();
        layer.init();
        this.addChild(layer);
    }
});


